package com.mojawa.mj_amap.bean

import android.os.Parcel
import android.os.Parcelable
import com.amap.api.maps.model.LatLng

data class PathRecord(
    var id: Long? = null,               // 主键ID
    var startPoint: LatLng? = null,     // 运动开始点
    var endPoint: LatLng? = null,       // 运动结束点
    var pathLinePoints: MutableList<LatLng> = mutableListOf(), // 运动轨迹点集合
    var distance: Double? = null,       // 运动距离
    var duration: Long? = null,         // 运动时长
    var startTime: Long? = null,        // 运动开始时间
    var endTime: Long? = null,          // 运动结束时间
    var calorie: Double? = null,        // 消耗卡路里
    var speed: Double? = null,          // 平均速度（公里/小时）
    var distribution: Double? = null,   // 平均配速（分钟/公里）
    var dateTag: String? = null         // 日期标记
) : Parcelable {

    /**
     * 添加单个轨迹点到pathLinePoints集合中
     */
    fun addPoint(point: LatLng) {
        pathLinePoints.add(point)
    }

    /**
     * 覆盖toString方法，用于打印运动记录的基本信息
     */
    override fun toString(): String {
        return "recordSize: ${pathLinePoints.size}, distance: $distance m, duration: $duration s"
    }

    /**
     * 实现describeContents方法，用于描述Parcelable内容
     */
    override fun describeContents(): Int {
        return 0
    }

    /**
     * 实现writeToParcel方法，用于将对象写入Parcel中
     */
    override fun writeToParcel(dest: Parcel, flags: Int) {
        dest.writeValue(id)
        dest.writeParcelable(startPoint, flags)
        dest.writeParcelable(endPoint, flags)
        dest.writeTypedList(pathLinePoints)
        dest.writeValue(distance)
        dest.writeValue(duration)
        dest.writeValue(startTime)
        dest.writeValue(endTime)
        dest.writeValue(calorie)
        dest.writeValue(speed)
        dest.writeValue(distribution)
        dest.writeString(dateTag)
    }

    /**
     * Companion object 用于创建CREATOR对象，负责从Parcel中创建PathRecord对象
     */
    companion object CREATOR : Parcelable.Creator<PathRecord> {
        override fun createFromParcel(parcel: Parcel): PathRecord {
            return PathRecord(parcel)
        }

        override fun newArray(size: Int): Array<PathRecord?> {
            return arrayOfNulls(size)
        }
    }

    /**
     * 私有构造函数，用于从Parcel中读取数据并初始化PathRecord对象
     */
    private constructor(parcel: Parcel) : this(
        id = parcel.readValue(Long::class.java.classLoader) as? Long,
        startPoint = parcel.readParcelable(LatLng::class.java.classLoader),
        endPoint = parcel.readParcelable(LatLng::class.java.classLoader),
        pathLinePoints = parcel.createTypedArrayList(LatLng.CREATOR) ?: mutableListOf(),
        distance = parcel.readValue(Double::class.java.classLoader) as? Double,
        duration = parcel.readValue(Long::class.java.classLoader) as? Long,
        startTime = parcel.readValue(Long::class.java.classLoader) as? Long,
        endTime = parcel.readValue(Long::class.java.classLoader) as? Long,
        calorie = parcel.readValue(Double::class.java.classLoader) as? Double,
        speed = parcel.readValue(Double::class.java.classLoader) as? Double,
        distribution = parcel.readValue(Double::class.java.classLoader) as? Double,
        dateTag = parcel.readString()
    )
}
